package org.laobai.shop.goods.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.laobai.shop.goods.entity.Spec;
import org.laobai.shop.goods.entity.SpecOption;
import org.laobai.shop.goods.mapper.SpecMapper;
import org.laobai.shop.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@DubboService
public class SpecServiceImpl implements SpecService {

	@Autowired
	private SpecMapper specMapper;

	@Override
	public PageInfo<Spec> page(int currPage, Spec spec) {
		PageHelper.startPage(currPage, 3);
		List<Spec> tmp = specMapper.specList(spec);
		return new PageInfo<>(tmp);
	}

	@Override
	public boolean saveSpec(Spec spec) {
		/*
		 * 1、 保存规格 2、获取规格ID 3、获取规格属性集合 4、循环赋值，保存
		 */
		specMapper.saveSpec(spec);
		Integer specId = spec.getId();

		List<SpecOption> options = spec.getOptions();

		options.forEach(item -> {
			if (!StringUtils.isEmpty(item.getOptionName())) {
				item.setSpecId(specId);
				// TODO 批量添加
				specMapper.saveOption(item);
			}
		});

		return true;
	}

	@Override
	public List<Spec> getSpecList() {
		return specMapper.list();
	}

	@Override
	public List<SpecOption> getOptionsBySpecId(Integer specId) {
		return specMapper.getOptionsBySpecId(specId);
	}

	@Override
	public List<Spec> getBySkuId(List<Integer> skuIds) {
		return specMapper.getBySkuId(skuIds);
	}

}
